import binarysearchtree.BinarySearchTree;
import demo1.HashBuck;
import demo1.HashBuck2;
import sun.reflect.generics.tree.Tree;

import java.util.*;

class Student {
    public String id;

    public Student(String id) {
        this.id = id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(id, student.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}

public class Test {


    public static void main13(String[] args) {
        int[] array = new int[]{1, 2, 2, 3, 4, 5, 6, 7, 4};
        Map<Integer, Integer> map = new HashMap<>();
        for (int x : array) {
            if (map.get(x) == null) {
                map.put(x, 1);
            } else {
                int val = map.get(x);
                map.put(x, val + 1);
            }
        }
        Set<Map.Entry<Integer, Integer>> set = map.entrySet();
        for (Map.Entry<Integer, Integer> entry: set) {
            if (entry.getValue() > 1) {
                System.out.println("key:" + entry.getKey() + " val:" + entry.getValue());
            }
        }
    }


    public static void main12(String[] args) {
        int[] array = new int[]{1, 2, 2, 3, 4, 5, 6, 7, 4};
        Set<Integer> set = new HashSet<>();
        for (int x : array) {
            if (!set.add(x)) {
                System.out.print(x + " ");
            }
        }
    }


    public static void main11(String[] args) {
        int[] array = new int[]{1, 2, 2, 3, 4, 5, 6, 7, 4};
        HashSet<Integer> hashSet = new HashSet<>();
        for (int x : array) {
            hashSet.add(x);
        }
        System.out.println(hashSet);
    }


    public static void main10(String[] args) {
        Student student1 = new Student("1234");
        System.out.println(student1.hashCode());
        Student student2 = new Student("1234");
        System.out.println(student2.hashCode());
        HashBuck2<Student, Integer> hashBuck2 = new HashBuck2<>();
        hashBuck2.put(student1, 10);
        System.out.println(hashBuck2.getValue(student2));

    }

    public static void main9(String[] args) {
        Student student1 = new Student("1234");
        System.out.println(student1.hashCode());
        Student student2 = new Student("1234");
        System.out.println(student2.hashCode());

        HashMap<Student, Integer> map = new HashMap<>();
        map.put(student1, 2);
        System.out.println(map.get(student2));
    }


    public static void main8(String[] args) {
        TreeSet<Student> set1 = new TreeSet<>();
        HashSet<Student> set2 = new HashSet<>();

        HashMap<Student, Integer> map1 = new HashMap<>();
        TreeMap<Student, Integer> map2 = new TreeMap<>();

        //set1.add(new Student());//ERROR
        //set1.add(null);//ERROR
        //set2.add(new Student());//可以实现的
        set2.add(null);//可以实现的

        //map1.put(new Student(),2);//可以实现的
        map1.put(null, 2);//可以实现的
        //map2.put(new Student(),2);//ERROR
        //map2.put(null,2);//ERROR
    }


    public static void main7(String[] args) {
        HashMap<String, Integer> map = new HashMap<>();
        //TreeMap<String,Integer> map = new TreeMap<>();
        map.put("abcd", 2);
        map.put("hello", 10);
        map.put("gao", 3);

        System.out.println(map.get("hello"));
        System.out.println(map);

        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        for (Map.Entry<String, Integer> entry : entrySet) {
            System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
        }
        //迭代器
        Iterator<Map.Entry<String, Integer>> it = entrySet.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }


        Object[] set = entrySet.toArray();
        //System.out.println(set); //[Ljava.lang.Object;@7f31245a
        //set为Object类型数组，Object中toString方法就是打印的地址
        for (int i = 0; i < set.length; i++) {
            System.out.println(set[i]);
        }
        Set<String> strings = map.keySet();
        System.out.println(strings);


        Collection<Integer> collection = map.values();
        System.out.println(collection);

    }


    public static void main6(String[] args) {
        HashBuck hashBuck = new HashBuck();
        hashBuck.put(1, 11);
        hashBuck.put(2, 22);
        hashBuck.put(5, 55);
        hashBuck.put(8, 88);
        hashBuck.put(11, 666);
        hashBuck.put(21, 777);
        hashBuck.put(31, 888);
        hashBuck.put(41, 999);
        hashBuck.put(51, 555);

        System.out.println(hashBuck.get(8));

        System.out.println("========");
    }


    public static void main5(String[] args) {
        TreeSet<String> set = new TreeSet<>();
        //HashSet<String> set = new HashSet<>();
        set.add("sunny");
        set.add("the");
        set.add("hello");
        set.add("sunny");
        System.out.println(set);

        Iterator<String> it = set.iterator();
        while (it.hasNext()) { //检测是否有下一个
            System.out.println(it.next());//打印的同时向下走
        }
        Object[] strings = set.toArray();
        for (int i = 0; i < strings.length; i++) {
            System.out.println(strings[i]);
        }

        for (Object str : strings) {
            System.out.println(str);
        }


        /*//将set中的元素转换为数组返回
        Object[] ret = set.toArray();
        System.out.println(Arrays.toString(ret));

        System.out.println(set.remove("that"));
        List<String> list = new LinkedList<>();
        list.add("the");
        list.add("hello");
        list.add("run");

        System.out.println(set.containsAll(list));
        System.out.println(set.addAll(list));
        System.out.println(set);*/


        //set.clear();
        //System.out.println(set);
    }


    public static void main4(String[] args) {
        Map<Student, Integer> map = new TreeMap<>();
        //map.put(new Student(),1);
        //map.put(new Student(),2);

    }

    public static void main3(String[] args) {
        TreeMap<String, Integer> map = new TreeMap<>();
        map.put("sunny", 3);
        map.put("the", 5);
        map.put("hello", 2);
        map.put("sunny", 6);
        //map.put(null,18);
        System.out.println(map);

        //打印所有键值对
        //entrySet(): 将map中的键值对放在Set中返回了
        Set<Map.Entry<String, Integer>> entrySet = map.entrySet();
        for (Map.Entry<String, Integer> entry : entrySet) {
            System.out.println("key: " + entry.getKey() + " val: " + entry.getValue());
        }


       /* //通过key获得val值
        Integer val1 = map.get("sunny");
        System.out.println(val1);
        Integer val2 = map.get("the");
        System.out.println(val2);
        //通过key获得val值，如果key不存在，返回默认值
        Integer val3 = map.getOrDefault("that",999);
        System.out.println(val3);

        //根据key来删除，并返回对应的val值
        Integer val4 = map.remove("sunny");
        System.out.println(val4);

        Set<String> set = map.keySet();
        System.out.println(set);

        Collection<Integer> collection = map.values();
        System.out.println(collection);

        System.out.println(map.containsKey("hello"));
        System.out.println(map.containsValue(12));*/

    }


    public static void main2(String[] args) {
        Map<String, Integer> map1 = new TreeMap<>();//查找的时间复杂度O(logN)
        Map<String, Integer> map2 = new HashMap<>();//查找的时间福再度O(1), 底层是哈希表 -》数组+链表+红黑树组成的

        Set<String> set1 = new TreeSet<>();
        Set<String> set2 = new HashSet<>();
    }


    public static void main1(String[] args) {
        BinarySearchTree binarySearchTree = new BinarySearchTree();
        int[] array = new int[]{5, 12, 3, 2, 11, 15};
        for (int i = 0; i < array.length; i++) {
            binarySearchTree.insert(array[i]);
        }
        binarySearchTree.remove(12);


        /*System.out.println(binarySearchTree.search(11));
        System.out.println(binarySearchTree.search(222));*/

        System.out.println("===========");
    }
}
